# -*- coding: utf-8 -*-
from django.core.exceptions import PermissionDenied
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from main.models import Employee
from promotion.models import AwardedEmployee, Promotion

class PromotedList(ListView):
    template_name = 'promotion/promoted.html'
    def get_queryset(self):
        q=[]
        for aw in Promotion.objects.all():
            for item in AwardedEmployee.objects.filter(promotion=aw):
                if item.employee not in q:
                    q.append(item.employee)
        return q

    def get_context_data(self, **kwargs):
        if not self.request.user.has_perm("promotion.can_view_apromotion"):
            raise PermissionDenied
        return super(PromotedList, self).get_context_data(**kwargs)

class PromotedDetail(DetailView):
    template_name = 'promotion/promoted_detail.html'
    model = Employee

    def get_context_data(self, **kwargs):
        if not self.request.user.has_perm("promotion.can_view_apromotion"):
            raise PermissionDenied
        return super(PromotedDetail, self).get_context_data(**kwargs)

class PromotionsList(ListView):
    template_name = 'promotion/promotions.html'
    queryset = Promotion.objects.all()

    def get_context_data(self, **kwargs):
        if not self.request.user.has_perm("promotion.can_view_promotion"):
            raise PermissionDenied
        return super(PromotionsList, self).get_context_data(**kwargs)

class PromotionDetail(DetailView):
    template_name = 'promotion/promotion_detail.html'
    queryset = Promotion.objects.all()

    def get_context_data(self, **kwargs):
        if not self.request.user.has_perm("promotion.can_view_promotion"):
            raise PermissionDenied
        return super(PromotionDetail, self).get_context_data(**kwargs)